package com.jams.goodox.activity.serve.repair;

import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;

import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.activity.serve.ServeStopTime;
import com.jams.goodox.adapter.RepairAdapter;
import com.jams.goodox.constant.Constant;
import com.jams.goodox.entity.RepairBean;
import com.jams.goodox.entity.RepairListBean;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.ServiceService;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.ToastUtil;
import com.jams.goodox.widget.NoScrollGridView;
import com.jams.library.commonAdapter.ListCommonAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import retrofit2.Call;

/**
 * Created by jams_zhen on 2016/9/13.
 * use for 维修列表
 */
public class RepairActivity extends BaseActivity {
    @Bind(R.id.linear_company)
    LinearLayout linearCompany;
    @Bind(R.id.grid_company_service)
    NoScrollGridView gridCompanyService;

    @Bind(R.id.linear_public)
    LinearLayout linearPublic;
    @Bind(R.id.grid_public_service)
    NoScrollGridView girdPublicService;

    @Bind(R.id.linear_repair)
    LinearLayout linearRepair;

    private ListCommonAdapter<RepairBean> pubRepairAdapter, companyRepairAdapter;
    private List<RepairBean> pubRepairLists, companyRepairLists;

    private String subId;
    private String barTitle;
    private String busId;

    @Override
    protected int getLayoutId() {
        return R.layout.repair_activity;
    }

    @Override
    protected void init() {
        subId=getIntent().getStringExtra("subId");
        barTitle = getIntent().getStringExtra("title");
        busId=getIntent().getStringExtra("busId");
        StyleUtil.titleBackKey(this, barTitle);
        companyRepairLists = new ArrayList<>();
        gridCompanyService.setAdapter(companyRepairAdapter = new RepairAdapter(this, companyRepairLists));
        gridCompanyService.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AddRepairActivity.go(RepairActivity.this,Constant.SERVE_TYPE_COMPANY,companyRepairLists.get(position).getServiceName(),companyRepairLists.get(position).getId(),subId, barTitle,busId);
            }
        });

        pubRepairLists = new ArrayList<>();
        girdPublicService.setAdapter(pubRepairAdapter = new RepairAdapter(this, pubRepairLists));
        girdPublicService.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AddRepairActivity.go(RepairActivity.this, Constant.SERVE_TYPE_PUBLIC,pubRepairLists.get(position).getServiceName(),pubRepairLists.get(position).getId(),subId, barTitle,busId);
            }
        });

        ServeStopTime serveStopTime=new ServeStopTime(this);
        serveStopTime.init(subId);//请求服务暂停时间

        getDate();
    }

    private void getDate() {
        showLoading();
        enqueue(createCallService(ServiceService.class).getRepairList(subId), new RetrofitCallbackListener<RepairListBean>() {
            @Override
            public void onSuccess(Call<RepairListBean> call, RepairListBean repairListBean) {
                misLoading();
                if (repairListBean.getData() == null) return;
                linearRepair.setVisibility(View.VISIBLE);
                if(repairListBean.getData().getPublicService()!=null&&repairListBean.getData().getPublicService().size()>0){
                    linearPublic.setVisibility(View.VISIBLE);
                    pubRepairLists.clear();
                    pubRepairLists.addAll(repairListBean.getData().getPublicService());
                    pubRepairAdapter.notifyDataSetChanged();
                }else {
                    linearPublic.setVisibility(View.GONE);
                }
                if(repairListBean.getData().getHouseService()!=null&&repairListBean.getData().getHouseService().size()>0){
                    linearCompany.setVisibility(View.VISIBLE);
                    companyRepairLists.clear();
                    companyRepairLists.addAll(repairListBean.getData().getHouseService());
                    companyRepairAdapter.notifyDataSetChanged();
                }else {
                    linearCompany.setVisibility(View.GONE);
                }
            }

            @Override
            public void onError(String msg) {
                ToastUtil.show(msg);
                misLoading();
            }
        });
    }

}
